﻿using System;
using System.Collections.Generic;
using BCEntity.OA.Attendance.AttendanceWiFi;
using BCData.OA.Attendance.AttendanceWiFi;
using BCDto.MySql.Admin;
using BCDto.OA.Attendance.AttendanceWiFi;
using BCService.Utils;

namespace BCService.OA.Attendance.AttendanceWiFi
{
    /// <summary>
    /// 考勤Wifi信息
    /// </summary>
    public class AttendanceWiFiService : IAttendanceWiFiService
    {
        private readonly IAttendanceWiFiData attendanceWiFiData;

        public AttendanceWiFiService(IAttendanceWiFiData attendanceWiFiData)
        {
            this.attendanceWiFiData = attendanceWiFiData;
        }

        /// <summary>
        /// 新创建WiFi信息
        /// </summary>
        /// <param name="admin">当前登录管理员</param>
        /// <param name="dto">请求的WiFi信息</param>
        /// <returns></returns>
        public AttendanceWiFiDto Create(AdminDto admin, AttendanceWiFiRequestDto dto)
        {
            //校验是否已存在
            if (attendanceWiFiData.CheckIsExist(dto.MAC).GetAwaiter().GetResult())
            {
                throw new ArgumentException("已存在该MAC地址的WiFi信息");
            }
            AttendanceWiFiEntity entity = dto.As<AttendanceWiFiEntity>();
            entity.AdminId = admin.AdminId;
            entity.AdminName = admin.FullName;
            return attendanceWiFiData.InsertAsync(entity).GetAwaiter().GetResult().As<AttendanceWiFiDto>();
        }

        /// <summary>
        ///删除考勤WiFi信息
        /// </summary>
        /// <param name="admin">当前管理员</param>
        /// <param name="id">id</param>
        /// <returns></returns>
        public int Delete(AdminDto admin, long id)
        {
            //校验是否已存在
            if (attendanceWiFiData.CheckIsExist(id).GetAwaiter().GetResult())
            {
                throw new ArgumentException("WiFi信息不存在或已作废");
            }
            return attendanceWiFiData.DeleteAsync(id, admin.FullName).GetAwaiter().GetResult();
        }

        /// <summary>
        /// 获取考勤WiFi信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public AttendanceWiFiDto Get(long id)
        {
            return attendanceWiFiData.GetEntityAsync(id).GetAwaiter().GetResult().As<AttendanceWiFiDto>();
        }

        /// <summary>
        /// 获取考勤WiFi信息列表
        /// </summary>
        /// <returns></returns>
        public List<AttendanceWiFiDto> GetList()
        {
            return attendanceWiFiData.GetAsync().As<List<AttendanceWiFiDto>>();
        }

        /// <summary>
        /// 获取考勤WiFi信息
        /// </summary>
        /// <param name="groupId">考勤组Id</param>
        /// <returns></returns>

        public List<AttendanceWiFiDto> GetListByModeIdAndGroupId(long groupId)
        {
            return attendanceWiFiData.GetListByModeIdAndGroupId(groupId).GetAwaiter().GetResult().As<List<AttendanceWiFiDto>>();
        }

        /// <summary>
        /// 修改指定的WiFi信息
        /// </summary>
        /// <param name="admin">当前管理员</param>
        /// <param name="id">id</param>
        /// <param name="dto">修改信息</param>
        /// <returns></returns>
        public AttendanceWiFiDto Update(AdminDto admin, long id, AttendanceWiFiRequestDto dto)
        {
            //校验是否已存在
            if (attendanceWiFiData.CheckIsExist(id).GetAwaiter().GetResult())
            {
                throw new ArgumentException("WiFi信息不存在或已作废");
            }
            AttendanceWiFiEntity entity = dto.As<AttendanceWiFiEntity>();
            entity.ModifierId = admin.AdminId;
            entity.ModifierName = admin.FullName;
            return attendanceWiFiData.UpdateAsync(entity).GetAwaiter().GetResult().As<AttendanceWiFiDto>();
        }
    }
}
